var app = getApp();

Page({
    data: {
        orderId:"",
        orderType:"",
        email: "",
        mobile: "",
        address:"",
    },
    onLoad: function (e) {
        var that = this;
        that.setData({
            navH: app.globalData.navHeight,
            marH: app.globalData.navHeight+20,
            orderId:e.orderId,
            orderType:e.orderType
        })
        that.getOrderContact();
    },
    navBack: function () {
        wx.navigateBack({
            delta: 1
        })
    },
    getOrderContact: function () {
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/order/orderDetails",
            data: {
                order_id: that.data.orderId,
                order_type: that.data.orderType
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': 'token=' + wx.getStorageSync('token')
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var detData = res.data.data;
                    if(typeof(detData.address)==null){
                       var addressData="";
                    }else{
                        var addressData=detData.address;
                    }
                    that.setData({
                        email: detData.email,
                        mobile: detData.mobile,
                        address: addressData
                    })
                }
            },
            
        });
    },
    mobileInput:function(e){
        this.setData({
            mobile: e.detail.value
        })
    },
    emailInput:function(e){
        this.setData({
            email: e.detail.value
        })
    },
    addressInput:function(e){
        this.setData({
            address: e.detail.value
        })
    },
    formSubmit: function (e) {
        var that=this;
        var telReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        var emailRge = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (e.detail.value.mobile.length == 0 || e.detail.value.email.length == 0) {
            wx.showToast({
                title: '手机号码和邮箱不得为空',
                icon: 'none',
                duration: 1500
            })
        }else if(e.detail.value.mobile.length!=11){
            wx.showToast({
                title: '手机号码长度有误',
                icon: 'none',
                duration: 1500
            })
        }else if(!telReg.test(that.data.mobile)){
            wx.showToast({
                title: '手机号码格式不正确',
                icon: 'none',
                duration: 1500
            })
        }else if(!emailRge.test(that.data.email)){
            wx.showToast({
                title: '邮箱格式不正确',
                icon: 'none',
                duration: 1500
            })
        }else{
            //提交表单
            wx.request({
                url: app.globalData.apiHost + "/api/order/updateOrderContacts",
                data: {
                    mobile:e.detail.value.mobile,
                    email:e.detail.value.email,
                    address:e.detail.value.address,
                    order_id:that.data.orderId,
                },
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Cookie': 'token=' + wx.getStorageSync('token')
                },
                method: "POST",
                dataType: "json",
                success: function (res) {
                    if (res.statusCode === 200) {
                        wx.showToast({
                            title: '修改成功',
                            icon: 'success',
                            duration: 1500
                        })
                    }
                },
                fail: function (e) {
                },
                complete: function (obj) {
                }
            });
        }
    },
})
